﻿using ProjScan.GUIExtensions;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

//
// 摘要:
//     Drawer for the Table Attribute. See the TableAttribute class documentation for
//     the limitations of this attribute.
[CustomPropertyDrawer(typeof(TableAttribute))]
public class TableDrawer : PropertyDrawer
{
    private GUITableState tableState;

    private Rect lastRect;

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        Match match = Regex.Match(property.propertyPath, "^([a-zA-Z0-9_]*).Array.data\\[([0-9]*)\\]$");
        if (!match.Success)
        {
            EditorGUI.LabelField(position, label.text, "Use the Table attribute with a collection.");
            return;
        }

        string value = match.Groups[1].Value;
        string value2 = match.Groups[2].Value;
        if (!(value2 != "0"))
        {
            if (GUILayoutUtility.GetLastRect().width > 1f)
            {
                lastRect = GUILayoutUtility.GetLastRect();
            }

            Rect screenRect = new Rect(lastRect.x + 15f, lastRect.y + 35f, lastRect.width, lastRect.height);
            GUILayout.BeginArea(screenRect);
            EditorGUI.indentLevel = 0;
            tableState = GUITable.DrawTable(property.serializedObject.FindProperty(value), tableState);
            GUILayout.EndArea();
            GUILayout.Space(30f);
        }
    }
}